﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YREQBOOK : UserControl
    {
        #region-------------Properties-----------------------

        YGRABDOCData MainViewModel;
        bool IsLoaded = false;
        ITXEXRPT ITXEXRPT;
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public SETINTEXDB SetMaster
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetMaster");
            }
        }
        public SETINTEXDB SetDetail
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetDetail");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB SetStock
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetStock");
            }
        }

       

        #region=====vwGAAUF=======
        CollectionViewSource vwGAAUF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGAAUF");
            }
        }
        DataRow dwGAAUF
        {
            get
            {
                if (vwGAAUF.View == null)
                    return null;
                if ((DataRowView)vwGAAUF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGAAUF.View.CurrentItem).Row;
            }
        }
        #endregion
      

        public string KRS
        {
            get
            {
                return Helpers.Helper.Right("  " + txtAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return Helpers.Helper.Right("000000" + txtAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtAUFLFD.Text = value;
            }
        }
        string POS
        {
            get
            {
                return Helpers.Helper.Right("000" + txtAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtAUFPOS.Text = value;
            }
        }
 
 
  
        BindingListCollectionView cvYARNA
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.dlgYARNA.ItemsSource);
            }
        }
        BindingListCollectionView cvMGAAUF
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.dlgMGAAUF.ItemsSource);
            }
        }
        #endregion
        public YREQBOOK()
        {
            InitializeComponent();
            
 

        } 

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DesignerProperties.GetIsInDesignMode(this))
                    return;
                if (IsLoaded)
                    return;
                MainViewModel =DataContext as YGRABDOCData;
                if (MainViewModel == null)
                {
                    MainViewModel = new YGRABDOCData();
                }
                IsLoaded = true; 
                vwGAAUF.View.CurrentChanged += vwGAAUF_CurrentChanged;
                KRS = "HM";
                LFD = "000014";
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void txtAUFLFD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //btnQuery_Click(null, null);
            }
        }
        void vwGAAUF_CurrentChanged(object sender, EventArgs e)
        {
            if (dwGAAUF == null)
                return;
            var qTotal = (from GA in SetDetail.Tables["GAAUF"].AsEnumerable()
                          where GA["GASA"].ToString() == "713"
                          select Convert.ToDouble(GA["GABASMG2"].ToString())).Sum();
            double Total = (double)qTotal;
            //txtTRatio.Text = (Total / 1000).ToString();
            string filter = "GAART='" + dwGAAUF["GAART"].ToString() + "' AND GAFARBE='" + dwGAAUF["GAFARBE"].ToString() + "' AND GAAUFMSL='" + dwGAAUF["GAAUFMSL"].ToString() + "'";
            
        } 
    
      

        private void btnListMUReq_Click(object sender, RoutedEventArgs e)
        {
             MainViewModel.LoadMUReq(app.Mandant, txtKRSs.Text);
           
            cvYARNA.CustomFilter = null; 
        } 

        private void btnAddGAAUF_Click(object sender, RoutedEventArgs e)
        {
            if (dwGAAUF == null)
            {
                MessageBox.Show("请选择一条最相近的记录");
                return;
            }
            DataRow dwData = SetDetail.Tables["GAAUF"].Rows.Add();
            dwData.ItemArray = dwGAAUF.ItemArray;
            var gapos = (from DataRow row in SetDetail.Tables["GAAUF"].AsEnumerable()
                         select row["GAPOS"].ToString())
                      .Max();
            int POS = Convert.ToInt16((string)gapos);
            dwData["GAPOS"] = Helpers.Helper.Right("00" + (POS + 1), 2);
            if (vwGAAUF.View != null)
            {
                vwGAAUF.View.MoveCurrentToLast();
            }
        }
        private void btnSaveGAAUF_Click(object sender, RoutedEventArgs e)
        {
            if (dwGAAUF == null)
            {
                MessageBox.Show("请选择要保存的纱线");
                return;
            }
            foreach (DataRow row in SetDetail.Tables["GAAUF"].Rows)
            {
                row["GAART"] = Helpers.Helper.Left(row["GAART"].ToString().PadRight(20), 20);
                row["GAVAR"] = Helpers.Helper.Left(row["GAVAR"].ToString().PadRight(3), 3);
                row["GAFARBE"] = Helpers.Helper.Left(row["GAFARBE"].ToString().PadRight(10), 10);
                row["GABEM"] = Helpers.Helper.Left(row["GABEM"].ToString().PadRight(40), 40);
                DataSet SResult = INTEXDBC.QueryGSCOLOR(row, 5);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("纱线不存在，请核对编码");
                    continue;
                }
                INTEXDBC.SaveGAAUF(row, app.company);
            }
        } 
   
        private void txtRatio_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter && e.Key != Key.Return)
                return;
        }

        private void btnQuery_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            string filter = "GAART LIKE '" + txtART.Text.ToString().Trim() + "%' AND  GAFARBE LIKE '" + txtFARBE.Text.ToString() + "'%";
            cvMGAAUF.CustomFilter = filter;
        }

        private void btnFilterClear_Click(object sender, RoutedEventArgs e)
        {
            cvMGAAUF.CustomFilter = null;
        }
    }
}
